/**
   @file memory.h
   @brief Prototypes to use the memory workload.
*/

#ifndef WG_MEMORY_GENERATOR_HEADER
#define WG_MEMORY_GENERATOR_HEADER

#include <pthread.h>

#include "resource_model.h"

/** Left over from lmbench. */
#define MAX_MEM_PARALLELISM 1

/** The stride in reading from memory. */
#define STRIDE (4096/sizeof(char *))

/** Flags for initializing memory. */
#define GEN_SEQ 0x1
#define GEN_RAND 0x2

typedef struct mem_state mem_state;
typedef struct memory_generator_state memory_generator_state;

typedef unsigned long iter_t;

struct mem_state {
  char*	addr;	/* raw pointer returned by malloc */
  char*	base;	/* page-aligned pointer */
  char*	p[MAX_MEM_PARALLELISM];
  int	initialized;
  int	width;
  size_t	len;
  size_t	maxlen;
  size_t	line;
  size_t	pagesize;
  size_t	nlines;
  size_t	npages;
  size_t	nwords;
  size_t*	pages;
  size_t*	lines;
  size_t*	words;
};

/** The generator needs to keep track of a couple of memory states.  */
struct memory_generator_state {
  mem_state sequential;
  mem_state random;
};

memory_generator_state * initialize_memory(unsigned long, unsigned int);

/** Read bytes sequentially from a block of memory. */
int memory_read_bytes(mem_state *, unsigned long);

/** Write bytes sequentially to a block of memory. */
int memory_write_bytes(mem_state *, unsigned long);

/** Setup the memory workload. */
resource_model *setup_memory(cJSON *);
#endif
